<?php
namespace Plu\qdh\Services;

use Phalcon\DI\Injectable;
use Phalcon\DI;

class ServiceBase extends Injectable {

	public function getRedis($type) {
		$redis = $this->di->get("redis.{$type}");
		if ($redis) {
			return $redis;
		}

		return false;
	}

	static public function getCacheHandler() {
		return DI::getDefault()->get('cache');
	}

	public static function _getDI() {
		return DI::getDefault();
	}

	/**
	 * check array is number?
	 */
	public function toNumberList($ids) {
		$items = array();
		if (!is_array($ids)) {
			return $items;
		}

		for ($c = 0, $len = count($ids); $c < $len; $c++) {
			$id = $ids[$c];
			if ($id > 0) {
				$items[] = $id;
			}
		}

		return $items;
	}

	public function arrayToXml($arr) {
        $xml = "<xml>";
        foreach ($arr as $key => $val) {
            if (is_numeric($val)) {
                $xml .= "<" . $key . ">" . $val . "</" . $key . ">";
            } else
                $xml .= "<" . $key . "><![CDATA[" . $val . "]]></" . $key . ">";
        }
        $xml .= "</xml>";
        return $xml;
    }

}
?>
